﻿#
# Custom PowerShell object to represent an Employee/AD account.
# https://wdadsync.codeplex.com/
# 2014.08.07
#

#Region Custom PowerShell object to represent an Employee/AD account.
Function New-EmployeeObjectFromAD {
	[cmdletbinding()]
    Param(
		[parameter(Mandatory=$true)]
		[ValidateNotNullOrEmpty()]
		[PSObject]
		$employeeObjFromCSV,
		
		[parameter(Mandatory=$true)]
		[ValidateNotNullOrEmpty()]
		[ValidateLength(5,100)]
		[string]
		$domainQueryRoot
    )
    Process {
		# but what if ID is null???
		# probably log a failure, no?
		
		#
		# yes we are using the ID as taken from the CSV; it is critical for
		# this value to be valid.
		#
		$questUser = Find-ADUserByEmpID `
			$employeeObjFromCSV.EmployeeID `
			$domainQueryRoot
		
		#
		# this could be a new user without an Employee ID, yet in AD.
		# parse off logon and search AD again.
		#
		if(! $questUser) {
			$samAccountName = `
				Parse-SamAccountNameFromEmail $employeeObjFromCSV.Email
			$questUser = Find-ADUserByUserName $samAccountName $domainQueryRoot
		}
		
		#
		# still nothing was found; store an invalid user
		# object so the number of CSV and AD objects do not
		# get out of sync.
		#
		if(! $questUser) {
			$employeeObjectFromAD = New-Object PSObject
			Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
				-Name "InvalidRecord" -Value $true
			return($employeeObjectFromAD)
		}
	
		$employeeObjectFromAD = New-Object PSObject
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "NTAccountName" -Value $questUser.NTAccountName
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "FirstName" -Value $questUser.givenName
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "LastName" -Value $questUser.sn
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "DisplayName" -Value $questUser.DisplayName
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "Title" -Value $questUser.Title
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "Manager" -Value $questUser.Manager
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "Email" -Value $questUser.Mail
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "Addr1" -Value $questUser.StreetAddress
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "City" -Value $questUser.City
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "Province" -Value $questUser.StateOrProvince
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "PostalCode" -Value $questUser.PostalCode
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "Country" -Value $questUser.c
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "Phone" -Value $questUser.PhoneNumber
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "Mobile" -Value $questUser.MobilePhone
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "EmployeeID" -Value $questUser.EmployeeID
		Add-Member -InputObject $employeeObjectFromAD -MemberType NoteProperty `
			-Name "OfficeNumber" -Value $questUser.physicalDeliveryOfficeName
		
		# nullify the Quest AD object - too much information to keep around
		$questUser = $null
		
		# return the new object
		return($employeeObjectFromAD)
    }	
}